<template>
<div id="background">
  <el-container>
    <el-header id="tiKuHead">
      <el-row type="flex" style="height:80%;display:flex;align-items: flex-end">
        <el-col :xs="2" :sm="2" :md="2" :offset="1" class="imgCol">
          <img v-bind:src="backIcon" class="backSize" @click="back"/>
        </el-col>
        <el-col :xs="6" :sm="6" :md="6" :offset="6" class="imgCol"> 
          <span class="titleSize">题库训练</span>
        </el-col>
        <el-col :xs="3" :sm="6" :md="6" :offset="5" class="imgCol">
          <span class="titleSize" @click="goToExam">测试</span>
        </el-col>
      </el-row>
    </el-header>
    <el-main style="backgroundColor:#ffffff">
 
        <van-tabs v-model="activeGongZhong" swipeable animated color="#53cdf5"  @change="showGongZhong" >
            <van-tab v-for="index in gongZhongs" v-bind:key="index.professionId" :title="index.professionName" :name="index.professionId">
                   
              </van-tab>
        </van-tabs>
     
      <div>
                                 <div v-for="tiKu in tiKus" :key="tiKu.id" style="margin-top:10px" @click="goLianXi(tiKu.id)"  v-infinite-scroll="load" infinite-scroll-disabled="disabled">
                                        <el-col :xs="12" :sm="12" :md="12">
                                            <el-card style="margin-left: 10px; margin-right: 10px;margin-top:10px;height:130px" :body-style="{ padding: '0px'}" >
                                                <div  style="position:relative;text-align:center;">
                                                     <el-image
                                                        style="width: 100%; height: 85px;border-radius:5px"
                                                        :src="tiKu.picture"
                                                        fit="cover">
                                                    </el-image>
                                                    <div style="position:absolute;left:0;top:60px;right:0;bottom:0">
                                                        <el-row type="flex" justify="start">
                                                            <el-col :xs="16" :sm="16" :md="16" >
                                                                <span class="tiKuSmallWords words">提供者:{{tiKu.provider}}</span>
                                                            </el-col>
                                                            <el-col v-if="tiKu.tag" :xs="6" :sm="6" :md="6" :offset="2">
                                                                <span class="reMenTag">热门</span>
                                                            </el-col>
                                                            <el-col v-else :xs="6" :sm="6" :md="6" :offset="2">
                                                                <span class="zuiXinTag">最新</span>
                                                            </el-col>
                                                        </el-row>
                                                    </div>
                                                </div>
                                                <div>
                                               <el-row >
                                                    <el-col   class="noHuanHang imgCol" >
                                                       <span class="words">{{tiKu.name}}</span>
                                                    </el-col>
                                                </el-row>
                                                <el-row type="flex">
                                                    <el-col :xs="15" :sm="15" :md="15" :offset="1">
                                                        <span  style="color:#f16161" v-show="tiKu.price!=null">¥{{tiKu.price}}</span>
                                                    </el-col>
                                                    <el-col  :xs="7" :sm="7" :md="7" :offset="1">
                                                        <i class="el-icon-user-solid" style="font-size:12px;color:#707070"></i>
                                                         <span style="font-size:12px" class="words noHuanHang">{{tiKu.enrollCount}}</span>
                                                    </el-col>
                                       
                                                </el-row>
                                                </div>
                                            </el-card>
                                        </el-col>
                                </div>
      </div>
   
    </el-main>
        <div style="height:5px"></div>
        <div v-if="loading" class="imgCol">
            <span class="words" style="font-size:14px">加载中...</span>
        </div>
        <div v-if="noMore" class="imgCol">
            <span class="words" style="font-size:14px">没有更多了...</span>
        </div>
  </el-container>
</div>
</template>
<script>
import stickySlot from '../common/stickySlot/stickySlot.vue'
import backIcon from '../../assets/img/mine/geRenZhongXin/back.png'
import { Toast} from 'vant';
import { setTimeout } from 'timers';
export default {
  components:{
    stickySlot,
  },
  computed: {
      noMore () {
        return this.tiKus.length>=this.tiKuInfo.total
      },
      disabled () {
        return this.loading || this.noMore
      }
    },
  data(){
    return{
      //绑定加载状态是否显示
      loading: false,
      //绑定真题的名字
      zhenTi:'',
      info:[],
      backIcon:backIcon,
      normalHeight:document.documentElement.clientHeight,
      //绑定tab头的对应参数
      activeGongZhong:'',
      gongZhongs:[],
      //绑定refresh的对应参数
      data:[],
      isLoading: false,
      //绑定list的对应参数
      loading: false,
      finished: false,
        tiKus:[],
      tiKuInfo:{
        //对应工种的id
        id:1,
        //对应工种的名称
        name:'',
        //该工种下对应的题库总数
        total:0,
        professionId:0,
        pageNo:1,
        pageSize:8,
      },
      scrollTop:'',
      scrollHeight:'',
      windowHeight:''
    }
  },
  mounted(){
    
        this.getZhiYes();


        var bg = document.getElementById("background");
       this.normalHeight=document.documentElement.clientHeight;
        console.log("height:"+this.normalHeight);
        bg.style.height = this.normalHeight+"px";
        bg.style.backgroundColor = "#FFFFFF"
        bg.style.margin = "0px";
         var normalHeight=sessionStorage.getItem('normalHeight');
        var head=document.getElementById("tiKuHead");
        var headHeight=normalHeight/30 *4 ;
        console.log("headHeight:"+headHeight);
        head.style.height=headHeight+"px";
  },
  methods:{
      goLianXi(bankId){
          console.log('题库ID'+bankId);
        this.$router.push({
                path:'/tiKu/lianXiTiXing',
                query:{
                    bankId:bankId,
                }
            })
      },
      back(){
            console.log("back:");
            this.$router.go(-1);
        },
      goToExam(){
            this.$router.push({
                path:'/tiKu/examTixing',
                query:{
                    professionId:this.tiKuInfo.id,
                    name:this.tiKuInfo.name
                }
            })
      },
      //获取工种种类
      getZhiYes(){
            var URL=this.IP.IP+'/visitor/getProfessionInformation';
            console.log(URL);
            this.$ajax({
                method:'get',
                url:URL,
            }).then(response=>{
                console.log(response.data);
                var code=response.data.code;
                if(code==0){
                    this.gongZhongs=response.data.data.professionInformation;
                    this.tiKuInfo.id=this.gongZhongs[0].professionId;
                    this.tiKuInfo.name=this.gongZhongs[0].professionName;
                    this.getTiKus();
                }else{
                    Toast(response.data.msg);
                }
            }).catch(error=>{
                console.log(error);
                //Toast(error);
            })
      },
      //选择工种不同时触发
      showGongZhong(id,name){
          console.log("工种："+id+" "+name);
          this.finished=false;
          this.tiKus=[];
          this.tiKuInfo.id=id;
          this.tiKuInfo.name=name;
          this.tiKuInfo.pageNo=0;
          this.getTiKus();
      },
      //通过工种ID获取对应题库预览
      getTiKus(){
            var URL=this.IP.IP+'/questionBank/questionBanks';
            console.log(URL);
            console.log(this.tiKuInfo);
            this.$ajax({
                method:'get',
                url:URL,
                params:{
                    professionId:this.tiKuInfo.id,
                    pageNo:this.tiKuInfo.pageNo,
                    pageSize:this.tiKuInfo.pageSize
                }
            }).then(response=>{
                console.log(response.data);
                var code=response.data.code;
                if(code==0){
                    var i=0;
                    for(i=0;i<response.data.data.info.length;i++){
                        this.tiKus.push(response.data.data.info[i]);
                    }
                    console.log(this.tiKus);
                    this.tiKuInfo.total=response.data.data.total;
                    console.log(this.tiKuInfo.total);
                
                }else{
                   // Toast(response.data.msg);
                }
            }).catch(error=>{
                console.log(error);
                //Toast(error);
            })
        },
        load () {
            this.loading = true
            setTimeout(() => {
            this.tiKuInfo.pageNo++;
            this.getTiKus();
            this.loading = false;

            }, 2000)
        },
     
  }
}
</script>
<style>
.noHuanHang{
  white-space:nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.el-main{
    padding: 0px;
}
.imgCol{
    display:flex;
    align-items:center;
    justify-content:center
}
#tiKuHead{
    background-color: #53cdf5;
    color: #707070;
}
.tiKuSmallWords{
    color: #707070;
    font-size:8px;
}
.words{
    color:#909090;
}
.moneyCss{
  color: #f16161;
   /** color: #F16161;*/
}
.reMenTag{
  border: 1px solid #53cdf5;
  color: #53cdf5;
  background-color: rgb(255,255,255,0);
  font-size: 6px;
  border-radius: 3px;
  padding: 1px;
}
.zuiXinTag{
  border: 1px solid #F16161;
  color: #F16161;
  background-color: rgb(255,255,255,0);
  font-size: 6px;
  border-radius: 3px;
  padding: 1px;
}
.backSize{
    height: 26px;
}
.titleSize{
    font-size:1.125em;
    color:#ffffff
}
@media screen and (max-height: 600px){
  .backSize{
      height: 22px;
  }
  .titleSize{
      font-size: 1em;
  }
}
</style>
